package com.aode.playlist.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.aode.common.bean.response.Music;
import com.aode.playlist.R;
import com.aode.playlist.databinding.ItemMllistMusicBinding;

import java.util.ArrayList;
import java.util.List;

public class MusicAdapter extends RecyclerView.Adapter{
    private Context context;
    private ArrayList<Music> musicList;
    private MusicInterface musicInterface;
    public MusicAdapter(Context context, ArrayList<Music> musicArrayList, MusicInterface musicInterface){
        this.context = context;
        this.musicList = musicArrayList;
        this.musicInterface = musicInterface;
    }
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemMllistMusicBinding musicBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_mllist_music,parent,false);
        return new MusicHolder(musicBinding);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if(holder instanceof MusicHolder){
            MusicHolder musicHolder = (MusicHolder) holder;
            if(!musicList.isEmpty()&&musicList.size()>0){
                Music music = musicList.get(position);
                musicHolder.getMusicBinding().setMusic(music);
                musicHolder.getMusicBinding().setMusicInterface(musicInterface);
            }
        }
    }

    @Override
    public int getItemCount() {
        return musicList.size();
    }

    class MusicHolder extends RecyclerView.ViewHolder{
        private ItemMllistMusicBinding musicBinding;
        public MusicHolder(ItemMllistMusicBinding musicBinding) {
            super(musicBinding.getRoot());
            this.musicBinding = musicBinding;
        }

        public ItemMllistMusicBinding getMusicBinding() {
            return musicBinding;
        }

        public void setMusicBinding(ItemMllistMusicBinding musicBinding) {
            this.musicBinding = musicBinding;
        }
    }

    public interface MusicInterface{
        void onItemClick(Music music);
    }

}
